<?php
namespace app\index\model;
use app\index\model\Organization;
use think\Db;
use think\Model;

class Manage_model {
	public function getindex() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		//记录用户操作
		$data = [
			'user_id' => $uid, // 缺少返回user_id
			'log_action' => 'Manage/getindex', //默认2 临时工
			'log_desc' => '浏览账户管理', //默认空
			'create_time' => $time,
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		//查询有哪些角色以及人员 (授权)
		$cha = Db::table('cn_character')
			->field('chara_name')
			->select();
		// 循环遍历二维数组
		foreach ($cha as $v) {
			$character[] = $v['chara_name'];
		}
		$use = Db::table('cn_user')
			->field('user_name')
			->where('user_name', 'neq', 'admin')
			->select();
		// 循环遍历二维数组
		foreach ($use as $y) {
			$username[] = $y['user_name'];
		}

		// 查询有多少人员  以及对应的角色
		$user = Db::table('cn_user')
			->field('user_name')
			->where('user_name', 'neq', 'admin')
			->select();
		foreach ($user as $z) {
			$name[] = $z['user_name'];
		}
		$a = implode(',', $name);
		$all = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->field('cc.chara_name,cu.user_name')
			->where('cu.user_name', 'in', $a)
			->where('user_name', 'neq', 'admin')
			->select();

		//获得所有人员 以及对应的组织单位  和  角色
		$renyuan = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_user_organization_relation cuor', 'cuor.user_id = cu.user_id')
			->join('cn_organization co', 'co.org_id = cuor.org_id')
			->field('cu.user_name,cc.chara_name,co.org_name,cu.user_id')
			->where('cu.is_delete', 'eq', '0')
			->where('co.is_delete', 'eq', '0')
			->where('co.org_parent_id', 'eq', '0')
			->order('cc.chara_id')
			->select();
		// var_dump($renyuan);die;
		// 获得所有部门  以及部门的人数(人数没搞定)
		$bumen = Db::table('cn_organization')
			->alias('co')
			->join('cn_user_organization_relation cuor', 'cuor.org_id = co.org_id')
			->field('co.org_name,co.org_type,co.org_id,count("co.org_id") as count')
			->where('co.org_parent_id = 0')
			->where('co.is_delete = 0')
			->where('cuor.is_delete = 0')
			->group('co.org_name')
			->select();
		// var_dump($character);die;
		// ->paginate(10);
		// var_dump($bumen);die;
		// $bmpage = $bumen->render();

		$ttt = [
			'node' => $list,
			'cha' => $character,
			'use' => $username,
			'all' => $all,
			'renyuan' => $renyuan,
			'bumen' => $bumen,
			// 'rypage' => $rypage,
			// 'bmpage' => $bmpage
		];

		// var_dump($ttt);die;
		return $ttt;

	}

	public function getryedit($id) {
		// var_dump($id);die;
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		//记录用户操作
		$data = [
			'user_id' => $uid,
			'log_action' => 'Manage/ryedit',
			'log_desc' => '编辑人员',
			'create_time' => $time,
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		//选择的人员的信息 (组织的id  )
		$choosery = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_organization_relation cuor', 'cuor.user_id = cu.user_id')
			->where('cu.user_id', $id)
			->field('user_name,user_email,user_working_place,user_mobile,user_office_phone,user_remarks,cuor.org_id,level')
			->select();
		// dump($choosery);die;
		//获得所属组织结构
		// $bmparent = Db::table('cn_organization')
		//         ->alias('co')
		//         ->where('is_delete','eq','0')
		//         ->where('org_parent_id','eq','0')
		//         ->field('org_name,org_id')
		//         ->select();
		$bmparent = Db::table('cn_organization')
			->where('is_delete', '0')
			->orderRaw('concat(path,org_id,",")')
			->select();
		$arr = array();
		// dump($bmparent);die;
		foreach ($bmparent as $k => $v) {
			// 统计多少级分类
			$level = substr_count($v['path'], ','); // 统计','出现的次数
			// var_dump($level);die;

			$v['level'] = $level;
			// var_dump($v);die;
			// dump($level-1);die;
			$v['html'] = str_repeat('---', $level - 1); // str_repeat(重复字符串,重复次数)

			// dump($v['html']);die;
			$arr[] = $v;
		}
		// dump($arr);die;
		$ttt = [
			'node' => $list,
			'id' => $id,
			'choosery' => $choosery,
			// 'bmtype' => $bmtype,
			'bmparent' => $arr,
		];
		return $ttt;

	}

	public function getrydoedit() {
		// var_dump($_POST);die;
		$id = $_POST['id'];
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$user_name = $_POST['user_name'];
		$user_email = $_POST['user_email'];
		$zuzhi = $_POST['zuzhi'];
		$user_working_place = $_POST['user_working_place'];
		$user_mobile = $_POST['user_mobile'];
		$user_office_phone = $_POST['user_office_phone'];
		$user_remarks = $_POST['user_remarks'];
		$level = $_POST['level'];
		//是否有重名
		$is_exist = Db::table('cn_user')
			->where('user_name', $user_name)
			->where('user_id', 'neq', $id)
			->field('user_id')
			->find();
		if ($is_exist) {
			return false;
		}
		// var_dump($user_name);die;
		//
		$udata = [
			'user_name' => $user_name,
			'user_email' => $user_email,
			'user_working_place' => $user_working_place,
			'user_mobile' => $user_mobile,
			'user_office_phone' => $user_office_phone,
			'user_remarks' => $user_remarks,
			'update_user_id' => $uid,
			'update_time' => $time,
			'level' => $level,
		];
		$ures = db('_user')
			->where('user_id', $id)
			->update($udata);
		// var_dump($ures);die;
		// cn_user_organization_relation
		// if ($zuzu =='') {
		//     if ($zu == '') {
		//         if ($zuzhi == '') {
		//             $orgin = Db::table('cn_user_organization_relation cuor')
		//                         ->where('user_id',$id)
		//                         ->field('org_id')
		//                         ->find();
		//             $cuordata = [
		//                         'org_id' => $orgin['org_id']
		//                 ];
		//             }else{
		//                 $cuordata = [
		//                         'org_id' => $zuzhi
		//                 ];
		//                 $orgin = Db::table('cn_user_organization_relation cuor')
		//                         ->where('user_id',$id)
		//                         ->field('org_id')
		//                         ->find();
		//                 if ($zuzhi == $orgin['org_id']) {
		//                     return true;
		//                 }
		//                 // var_dump($cuordata);die;
		//             }
		//         }else{
		//             // zuzhi不为空
		//             $cuordata = [
		//                         'org_id' => $zu
		//                 ];
		//        }

		// }else{
		//     // zuzhi不为空
		//          $cuordata = [
		//                     'org_id' => $zuzu
		//             ];

		// }

		$cuordata = [
			'org_id' => $zuzhi,
		];
		// var_dump($cuordata);die;
		$cuorres = db('_user_organization_relation')
			->where('user_id', $id)
			->update($cuordata);
		if ($ures || $cuorres) {
			return true;
		} else {
			return false;
		}

	}

	public function getbmedit($id) {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		//记录用户操作
		$data = [
			'user_id' => $uid, // 缺少返回user_id
			'log_action' => 'Manage/bmedit',
			'log_desc' => '编辑部门',
			'create_time' => $time,
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);
		$uid = $_SESSION['think']['username'][0]['user_id'];

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		// var_dump($list);die;
		//选择的部门的信息
		$choosebm = Db::table('cn_organization')
			->alias('co')
			->where('co.org_id', $id)
			->field('org_name,org_desc,org_address,org_type,org_parent_id,org_id')
			->find();
		// var_dump($choosebm);die;
		if ($choosebm['org_parent_id'] == '0') {
			$choosebm['org_id'] = '0';
		}
		// var_dump($choosebm);die;
		$bmparent = Db::table('cn_organization')
			->where('is_delete', '0')
			->orderRaw('concat(path,org_id,",")')
			->select();
		// var_dump($bmparent);die;
		$arr = array();
		foreach ($bmparent as $k => $v) {
			// 统计多少级分类
			$level = substr_count($v['path'], ','); // 统计','出现的次数
			$v['level'] = $level;
			$v['html'] = str_repeat('---', $level - 1); // str_repeat(重复字符串,重复次数)
			$arr[] = $v;
		}
		$ttt = [
			'node' => $list,
			'id' => $id,
			'choosebm' => $choosebm,
			// 'bmtype' => $bmtype,
			'bmparent' => $arr,
		];
		return $ttt;
	}

	public function getbmdoedit($id) {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$org_name = $_POST['org_name'];
		$org_desc = $_POST['org_desc'];
		$org_address = $_POST['org_address'];
		$org_type = $_POST['org_type'];

		// 部门可转移才打开
		// $org_parent_id = $_POST['org_parent_id'];
		// if ($org_parent_id == '总分支') {
		// 	$org_parent_id = '0';
		// }
		$odata = [
			'org_name' => $org_name,
			'org_desc' => $org_desc,
			'org_address' => $org_address,
			'org_type' => $org_type,
			// 'org_parent_id' => $org_parent_id,
			// 'org_parent_id' => $org_parent_id,
			// 'org_parent_id' => $org_parent_id,
			// 'org_parent_id' => $org_parent_id,
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
			'is_delete' => '0',
		];
		$ores = db('_organization')
			->where('org_id', $id)
			->update($odata);

		return $ores;

	}

	public function getryadd() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		//记录用户操作
		$data = [
			'user_id' => $uid, // 缺少返回user_id
			'log_action' => 'Manage/ryadd',
			'log_desc' => '新增人员', //默认空
			'create_time' => date('Y-m-d H:i:s', time()),
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		// var_dump($list);die;
		//查询有多少组织病排列
		// $org = Db::table('cn_organization')
		//         ->alias('co')
		//         ->where('is_delete','0')
		//         ->field('org_name,org_id,org_parent_id')
		//         // ->order('org_id desc')
		//         ->select();
		//         var_dump($org);die;
		$org = Db::table('cn_organization')
			->where('is_delete', '0')
			->orderRaw('concat(path,org_id,",")')
			->select();
		// var_dump($org);die;
		$arr = array();
		foreach ($org as $k => $v) {
			// 统计多少级分类
			$level = substr_count($v['path'], ','); // 统计','出现的次数
			$v['level'] = $level;
			$v['html'] = str_repeat('---', $level - 1); // str_repeat(重复字符串,重复次数)
			$arr[] = $v;
		}
		// var_dump($arr);die;
		//     $b = [];
		// for ($i=0; $i <count($org) ; $i++) {
		//     if ($org[$i]['org_parent_id'] == 0) {
		//         $b[$org[$i]['org_id']] = $org[$i];
		//     }

		// }

		//查询组织下有多少子组织
		// $sorg = Db::table('cn_organization')
		//         ->alias('co')
		//         ->where('org_parent_id','NEQ','0')
		//         ->where('is_delete','0')
		//         ->field('org_name,org_id')
		//         ->select();
		$ttt = [
			'node' => $list,
			'org' => $arr,
			// 'sorg' => $sorg
		];
		return $ttt;
		// var_dump($ttt);die;
	}

	public function getsqzz() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d h:i:s', time());

		// 根据的获得post值 查询角色id 人员id
		$use[] = $_POST['use'];
		$cha[] = $_POST['cha'];
		$a = $use[0];
		$b = $cha[0];
		$c = '';
		// dump($a);
		// dump($b);

		// 查询人员角色id
		for ($i = 0; $i < count($a); $i++) {
			# code...
			$getsq[] = Db::table('cn_user_character_relation')
				->alias('cucr')
				->join('cn_user cu', 'cu.user_id = cucr.user_id')
				->where('cu.user_name', $a[$i])
				->field('cucr.chara_id,cucr.user_id')
				->select();
		}

		foreach ($getsq as $key => $value) {

			foreach ($value as $k => $v) {

				//     if ($v['chara_id'] == 0) {
				//         // dump(123);die;
				//     $data = [
				//         'chara_id' => $b[$k],
				//         'user_id' => $v['user_id'],
				//         'create_user_id' => $uid,
				//         'create_time' => $time,
				//         'update_user_id' => $uid,
				//         'update_time' => $time,
				//         'is_delete' => 0
				//     ];
				//     $new = Db::table('cn_user_character_relation')
				//                 ->insert($data);
				//     return $new;
				// }else{

				// $shan = Db::table('cn_user_character_relation')
				//                     ->where('chara_id',$v['chara_id'])
				//                     ->where('user_id',$v['user_id'])
				//                     ->delete();
				// foreach ($b as $kk => $vv) {
				$c = implode(",", $b);

				$data = [
					'user_id' => $v['user_id'],
					'chara_id' => $c,
					'create_user_id' => $uid,
					'create_time' => $time,
					'update_user_id' => $uid,
					'update_time' => $time,
					'is_delete' => 0,
				];
				// }

			}

			$new = Db::table('cn_user_character_relation')
				->where('user_id', $v['user_id'])
				->update($data);

			// }

		}
		return $new;
		// var_dump($new);die;
		//查选择的角色对应的chara_id
		// $getsq = Db::table('cn_character')
		//         ->alias('cc')
		//         ->where('cc.chara_name',$cha)
		//         ->field('chara_id')
		//         ->select();    //获得原来人员的角色id

		// var_dump($getsq);die;
		// for ($i = 0; $i < count($a); $i++) {
		// //查选择的人员对应的user_id
		//     $achsq[] = Db::table('cn_user')
		//             ->alias('cu')
		//             ->where('cu.user_name',$a[$i])
		//             ->field('user_id')
		//             ->find();
		//             //获得原来人员的角色id
		//     foreach ($achsq as $key => $value) {

		//             $ach[$value['user_id']][] = Db::table('cn_character')
		//                     ->alias('cc')
		//                     ->join('cn_user_character_relation cucr','cucr.chara_id = cc.chara_id')
		//                     ->where('cucr.user_id',$value['user_id'])
		//                     ->select();
		//             // $shan = Db::table('cn_user_character_relation')
		//             //         ->where('user_id',$value)
		//             //         ->delete();
		//     }
		//     for ($j = 0; $j < count($b) ; $j++) {
		//         //删除原来角色
		//         // 循环插入角色
		//         $ncha = [
		//             'user_id' => $achsq[$i]['user_id'],
		//             'chara_id' => $b[$j],
		//             'create_user_id' => $uid,
		//             'create_time' => $time,
		//             'update_user_id' => $uid,
		//             'update_time' => $time,
		//             'is_delete' => 0
		//         ];
		//         $upsq = Db::table('cn_user_character_relation')
		//             ->insert($ncha);
		//     }

		// }
		// dump($ach);die;
		// var_dump($upsq);die;
		// return $upsq;
	}

	public function getrydel($id) {
		$delr = [
			'is_delete' => '1',
		];
		$drres = Db::table('cn_user')
			->where('user_id', $id)
			->update($delr);
		//删除人员的角色
		$removery = Db::table('cn_user_character_relation')
			->where('user_id', $id)
			->update($delr);
		//删除人员的组织
		$remove = Db::table('cn_user_organization_relation')
			->where('user_id', $id)
			->update($delr);
		// var_dump($removery);die;
		if ($drres && $removery && $remove) {
			return true;
			// dump(111);die;
		} else {
			return false;
		}
	}

	public function getbmdel($id) {
		$data = Db::table('cn_organization')
			->where('path', 'like', '%,' . $id . ',%')
			->where('is_delete', 0)
			->select();
		//根据子部门id 来看有没有人
		if ($data) {
			return false;
		}
		$delo = [
			'is_delete' => '1',
		];
		$dores = Db::table('cn_organization')
			->where('org_id', $id)
			->update($delo);
		//找到删除部门的上一级
		$parent_org_id = Db::table('cn_organization')
			->where('org_id', $id)
			->field('org_parent_id')
			->find();
		// var_dump($parent_org_id);die;
		$poid = [
			'org_id' => $parent_org_id['org_parent_id'],
		];
		// var_dump($poid);die;
		//删除的部门下的人员  移除部门
		$removery = Db::table('cn_user_organization_relation')
			->where('org_id', $id)
			->update($poid);
		// var_dump($removery);die;
		return $dores;
	}

	public function getbmdoadd() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		//记录用户操作
		$data = [
			'user_id' => $uid, // 缺少返回user_id
			'log_action' => 'Manage/bmadd',
			'log_desc' => '新增部门', //默认空
			'create_time' => date('Y-m-d H:i:s', time()),
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		// 获取组织结构(org)
		$oname = Db::table('cn_organization')
			->where('is_delete', '0')
			->orderRaw('concat(path,org_id,",")')
			->select();
		// var_dump($org);die;
		$arr = array();
		foreach ($oname as $k => $v) {
			// 统计多少级分类
			$level = substr_count($v['path'], ','); // 统计','出现的次数
			$v['level'] = $level;
			$v['html'] = str_repeat('---', $level - 1); // str_repeat(重复字符串,重复次数)
			$arr[] = $v;
		}

		$ttt = [
			'node' => $list,
			'oname' => $arr,
			// 'sorg' => $sorg
		];
		return $ttt;
	}

	//子部门无限扩展
	public function getsonindex($id) {
		// dump($id);die;
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d h:i:s', time());
		//记录用户操作
		$data = [
			'user_id' => $uid,
			'log_action' => 'Manage/sonindex',
			'log_desc' => '浏览账户管理(分支)', //默认空
			'create_time' => $time,
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();

		//查询分支有哪些角色以及人员 (授权)
		$cha = Db::table('cn_character')
			->field('chara_name,chara_id')
			->where('chara_id', 'neq', '0')
			->where('is_delete',0)
			->select();
		// 循环遍历二维数组
		// foreach($cha as $v){
		//     $character[] = $v['chara_name'];
		// }
		// var_dump($character);die;
		$use = Db::table('cn_user')
			->field('user_name')
			->where('user_name', 'neq', 'admin')
			->select();

		// 循环遍历二维数组
		foreach ($use as $y) {
			$username[] = $y['user_name'];
		}

		// 查询分支有多少人员  以及对应的角色
		$user = Db::table('cn_user')
			->field('user_name')
			->where('user_name', 'neq', 'admin')
			->select();
		foreach ($user as $z) {
			$name[] = $z['user_name'];
		}
		$a = implode(',', $name);
		$all = Db::table('cn_character')
			->where('chara_id', 'neq', 0)
			->where('is_delete', 'eq', 0)
			->field('chara_id,chara_name')
			->select();
		// dump($all);die;
		// 在查询对应权限有多少人
		for ($z = 0; $z < count($all); $z++) {
			$qr[$z] = '';
			$qx[$z] = Db::table('cn_user_character_relation')
				->alias('cucr')
				->join('cn_user cu', 'cu.user_id = cucr.user_id')
				->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
				->where('cucr.chara_id', 'like', '%' . $all[$z]['chara_id'] . '%')
				->where('cu.is_delete', 'eq', 0)
				->where('cc.is_delete', 'eq', 0)
				->field('cu.user_name')
				->select();
			for ($x = 0; $x < count($qx[$z]); $x++) {
				$qr[$z] .= $qx[$z][$x]['user_name'] . ',';
			}
			$qr[$z] = rtrim($qr[$z], ",");
		}
		foreach ($all as $key => $value) {
			$all[$key]['user_name'] = $qr[$key];
		}
		$allchara = Db::table('cn_character')
			->alias('cc')
			->field('chara_name,chara_id')
			->where('is_delete', 'neq', '1')
		// ->where('chara_id','neq','3')
			->select();
		// var_dump($id);die;
		// 根据$id再获取组织下面组织的id
		//获得分支人员 以及对应的组织单位  和  角色
		// $renyuan = Db::table('cn_user')
		// 	->alias('cu')
		// 	->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
		// // ->join('cn_character cc','cc.chara_id = cucr.chara_id')
		// 	->join('cn_user_organization_relation cuor', 'cuor.user_id = cu.user_id')
		// 	->join('cn_organization co', 'co.org_id = cuor.org_id')
		// 	->where('cu.is_delete', 'eq', '0')
		// 	->where('co.is_delete', 'eq', '0')
		// 	->where('cu.user_name', 'neq', 'admin')
		// 	->where('co.org_id', 'eq', $id)
		// 	->field('cu.user_name,co.org_name,cu.user_id,cucr.chara_id')
		// 	->select();

		$renyuan_sql = "SELECT
							cu.user_name,
							co.org_name,
							cu.user_id,
							cucr.chara_id,
							cu.is_delete
						FROM
							cn_user AS cu
							LEFT JOIN cn_user_character_relation AS cucr ON cu.user_id = cucr.user_id
							LEFT JOIN cn_user_organization_relation AS cuor ON cuor.user_id = cu.user_id
							LEFT JOIN cn_organization AS co ON co.org_id = cuor.org_id
						WHERE
							co.org_id = " . $id . "
							OR co.path like '" . '%' . $id . '%' . "'
							AND co.is_delete = 0
							AND cu.user_name != 'admin'
							";
		$renyuan = Db::query($renyuan_sql);
		// dump($renyuan_sql);die;
		// dump(explode(",",$renyuan[0]['chara_id']));die;
		for ($i = 0; $i < count($renyuan); $i++) {
			$a = explode(",", $renyuan[$i]['chara_id']);
			$cn[$i] = '';
			for ($j = 0; $j < count($a); $j++) {
				$nn[$i][$j] = Db::table('cn_character')
					->where('chara_id', $a[$j])
					->field('chara_name')
					->find();
				$cn[$i] .= $nn[$i][$j]['chara_name'] . ',';
			}
			$cn[$i] = rtrim($cn[$i], ",");
			// $cn[$i]['chara_name'] = implode(',',$nn[$i]);
		}
		// dump($cn);
		foreach ($renyuan as $key => $value) {
			$renyuan[$key]['chara_name'] = $cn[$key];
		}
		// 获得所有部门  以及部门的人数(人数没搞定)
		$bumen = Db::table('cn_organization')
			->alias('co')
		// ->join('cn_user_organization_relation cuor','cuor.org_id = co.org_id')
			->field('co.org_name,co.org_type,co.org_id')
			->where('co.org_parent_id', $id)
			->where('co.is_delete = 0')
		// ->where('cuor.is_delete = 0')
		// ->group('co.org_name')
			->select();

		// var_dump($bumen);die;
		// ->paginate(10);
		// var_dump($bumen);die;
		// $bmpage = $bumen->render();
		$this->org_model = new Organization();
		$treedata = Db::table('cn_organization')
			->where('is_delete', 'eq', 0)
			->field('org_id,org_name,org_parent_id')
			->select();
		$org_data = $this->org_model->getListByCondition(['is_delete' => 0]);
		$org_tree = recursionCate2($org_data, 'org_id', 0, 'org_parent_id');
		$tree = createCateTree2($org_tree, 'org_id', 'org_parent_id', 'org_name');
		// dump($tree);die;
		$ttt = [
			'node' => $list,
			'cha' => $cha,
			'use' => $username,
			'all' => $all,
			'tree' => $tree,
			'renyuan' => $renyuan,
			'bumen' => $bumen,
			// 'page' => $page,
			'allchara' => $allchara,
			// 'rypage' => $rypage,
			// 'bmpage' => $bmpage
		];

		// var_dump($ttt);die;
		return $ttt;
	}

	public function getZu() {
		$id = $_POST['id'];
		// var_dump($id);die;
		$sorg = Db::table('cn_organization')
			->alias('co')
			->where('org_parent_id', 'EQ', $id)
			->where('is_delete', '0')
			->field('org_name,org_id')
			->select();
		// var_dump($sorg);die;
		return $sorg;
	}

	public function getactive($id) {
		$delr = [
			'is_delete' => '0',
		];
		$drres = Db::table('cn_user')
			->where('user_id', $id)
			->update($delr);
		//删除人员的角色
		$removery = Db::table('cn_user_character_relation')
			->where('user_id', $id)
			->update($delr);
		//删除人员的组织
		$remove = Db::table('cn_user_organization_relation')
			->where('user_id', $id)
			->update($delr);
		// var_dump($removery);die;
		if ($drres && $removery && $remove) {
			return true;
			// dump(111);die;
		} else {
			return false;
		}
	}

}
?>